package com.nerve.android.cloudoffice.app.activity.chart;

import java.util.ArrayList;
import java.util.List;

import org.nerve.android.NerveActivityGroup;
import org.nerve.android.annotation.Acvitity;
import org.nerve.android.annotation.ViewOnId;

import android.annotation.SuppressLint;
import android.os.AsyncTask;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.nerve.android.cloudoffice.R;
import com.nerve.android.cloudoffice.app.NavigationActivity;
import com.nerve.android.cloudoffice.app.group.IndexGroupActivity;
import com.nerve.android.cloudoffice.common.Const;
import com.nerve.android.cloudoffice.common.Reply;
import com.nerve.android.cloudoffice.domain.Record;
import com.nerve.android.cloudoffice.service.NetService;

@SuppressLint("HandlerLeak")
@Acvitity(layout=R.layout.activity_chart)
public class ChartActivity extends NavigationActivity{
	
	@ViewOnId(id=R.id.date)
	private TextView dateTV;
	@ViewOnId(id=R.id.message_empty)
	private TextView emptyTV;
	@ViewOnId(id=R.id.listContent)
	private ListView listView;
	
	private List<Record> recordList = new ArrayList<Record>();
	
	private List<Record> recordListAll = new ArrayList<Record>();
	
	private String dateRange = "";
	
	private ChartAdapter adapter;
	
	private Handler handler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			hideProgressDialog();
			
			switch (msg.what) {
			case WORKER_FAIL:
				toast(Const.ERROR);
				break;
			case WORKER_OK:
				dateTV.setText("统计时间： " + dateRange);
				
				tongji();
				
				adapter.notifyDataSetChanged();
				break;
			}
		}
	};
	
	private void tongji(){
		recordList.clear();
		
		Record rr = null;
		for(Record r:recordListAll){
			for(int i=0;i<recordList.size();i++){
				rr = recordList.get(i);
				
				if(rr.getUser().equals(r.getUser())){
					rr.setWeekDone(r.getWeekDone() + rr.getWeekDone());
					rr.setDayDone(r.getDayDone() + rr.getDayDone());
					rr.setMark(r.getMark() + rr.getMark());
					
					break;
				}
			}
			
			//到达这里，则在recordList 中没有找到相关的记录，就将R增加到recordList 中
			recordList.add(r);
		}
		
		Const.log("tongji完成：" + recordList.size());
	}
	
	@Override
	protected void initUI() {
		super.initUI();
		
		setNavTitle("得分统计");
		
		listView.setEmptyView(emptyTV);
		adapter = new ChartAdapter();
		listView.setAdapter(adapter);
		
		dateTV.setText("统计时间： " );
		emptyTV.setText("注意：\n1.显示的是上一周的得分统计，如果上一周没有统计则没有任何内容显示" +
				"\n2. 普通职员只能看到自己的得分，部门管理员可以看到自己部门成员的得分，总管理员可以看到全部的" +
				"\n3.具体的评分规则请查看帮助");
		
		new ChartWorker().execute();
	}
	
	@Override
	protected void onNavigateBackButtonClick() {
		NerveActivityGroup group = getActivityGroup(IndexGroupActivity.class);
		
		group.changeTab(IndexGroupActivity.HOME);
	}
	
	private class ChartAdapter extends BaseAdapter{

		@Override
		public int getCount() {
			return recordList.size();
		}

		@Override
		public Object getItem(int position) {
			return recordList.get(position);
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(int position, View v, ViewGroup parent) {
			Record r = recordList.get(position);
			if(v == null)
				v = View.inflate(ChartActivity.this, R.layout.activity_chart_item, null);
			
			((TextView)v.findViewById(R.id.name)).setText(r.getUser());
			((TextView)v.findViewById(R.id.day)).setText(r.getDayDone()+"");
			((TextView)v.findViewById(R.id.weekdone)).setText(r.getWeekDone()+"");
			((TextView)v.findViewById(R.id.mark)).setText(r.getMark()+"");
			
			return v;
		}
	}
	
	private class ChartWorker extends AsyncTask<Void, Void, Integer>{

		@Override
		protected Integer doInBackground(Void... params) {
			String url = Const.HOST + "task/recordList.htm";
			
			Reply re = NetService.get(url);
			if(re.isError()){
				Const.ERROR = re.getMessage().toString();
				return WORKER_FAIL;
			}
			
			try{
				recordListAll = JSON.parseArray(re.getMessage().toString(), Record.class);
				
				dateRange = re.getData().toString();
			}catch(Exception e){
				Const.ERROR = "读取数据出错，请联系技术员";
				return WORKER_FAIL;
			}
			
			return WORKER_OK;
		}
		
		@Override
		protected void onPostExecute(Integer result) {
			handler.sendEmptyMessage(result);
		}
	}
	
}